// index.js
import app from '@system.app';
import fetch from '@system.fetch'; //请求接口
import storage from '@system.storage'; //缓存接口
import prompt from '@system.prompt'; //弹窗接口
import router from '@system.router'; //路由接口
import utils from '../../common/utils.js';
//import box2d from '../../common/box2d/Box2D.js';
//import box2d from '../../common/box2d.js';
//import * as THREE from '../../common/three/build/three.module.js';
//import { OrbitControls } from '../../common/three/jsm/controls/OrbitControls.js';
//import { CSS2DRenderer, CSS2DObject } from '../../common/three/jsm/renderers/CSS2DRenderer.js';
//let camera,scene,renderer;
//let uniforms,geometry,material,mesh;
//let camera, scene, renderer, labelRenderer;

var mouseX = 0,
    mouseY = 0,
    lat = 0,
    lon = 0,
    phy = 0,
    theta = 0;

var windowHalfX = 500 / 2;
var windowHalfY = 500 / 2;
var startTime = Date.now();
//this.init();
//this.animate();

const ACTION_SYNC = 0;

export default {
    data: {
        descriptionFirstParagraph: '玩法简介:(点击简介开始游戏)',
        cartText: '合并相同的水果,可以生成一个新的水果, 直至出来西瓜;通过收集关卡奖励,进行通关, 通关后可以获得相应的奖励.',
        cartStyle: 'cart-text',
        isCartEmpty: true,
        url:'https://baidu.com/',
        responseData: 'NA',
        imageList: ['/common/food_000.JPG', '/common/food_001.JPG', '/common/food_002.JPG', '/common/food_003.jpg'],
        imageList2: ['/common/game_1.jpg', '/common/game_2.jpg', '/common/game_3.jpg'],

    },
    onInit(){
        //this.addCart();
        console.info('==================================================')
        //console.info(app);
        //console.info(cc.version)
        //console.info(ACTION_SYNC)
        //console.info(this.$app.$def.globalData.appVersion)
        this.fetch();
        console.info('==================================================')

    },

    pushPage:function() {
        router.push({
          uri: 'pages/list/loading',
          params: {
            data1: 'message',
            data2: {
              data3: [123, 456, 789]
            },
          },
        });
    },
    fetch: function () {
        var that = this;
        fetch.fetch({
            url: "https://www.baidu.com",
            success: function(res) {
                console.info("fetch success:" + JSON.stringify(res));
            },
            fail: function(code, data) {
                console.info("fetch fail:" + code)
                console.info("fetch fail:" + data)
            }
        });

        fetch.fetch({
          url: that.url, //默认支持https，如果要支持http，需要在config.json里增加network标签，属性标识 "usesCleartext": true。即：
          success: function(response) {
            console.info("fetch success");
            //that.responseData = JSON.stringify(response);
            that.responseData = response.data;
            //JSON.stringify(that.responseData);
            console.info("data="+that.responseData);
          },
            fail: function(code, data) {
                console.log("fail: " + code);
                console.log("fail: " + data);
            }
        });
    },

    swipeToIndex(index) {
        this.$element('swiperImage').swipeTo({index: index});

    },

    goto(id){
        var page = router.getState();
        console.log('current index = ' + page.index);
        console.log('current name = ' + page.name);
        console.info('current path = ' + page.path);

        //this.addCart();
        console.info('==================================================')
        console.info(app);
        //console.info(cc.version)
        console.info(ACTION_SYNC)
        console.info(this.$app.$def.globalData.appVersion)
        console.info('==================================================')
        this.fetch();


        router.push({
            uri: 'pages/list/loading',
        });





//        prompt.showDialog({
//            title: 'Title Info',
//            message: '确认跳转到'+id,
//            buttons: [
//                {
//                    text: 'button',
//                    color: '#666666',
//                },
//            ],
//            success: function(data) {
//                console.log('dialog success callback，click button : ' + data.index);
//                // index页面
//                router.push({
//                    uri: 'pages/list/loading',
//                });
//            },
//            cancel: function() {
//                console.log('dialog cancel callback');
//            },
//        });
    },
    get_web() {
        if (this.isCartEmpty) {
            this.cartStyle = 'cart-text-focus';
        }
        console.info('==================================================')
        console.info(app);
        //console.info(cc.version)
        console.info(ACTION_SYNC)
        console.info(this.$app.$def.globalData.appVersion)
        console.info('==================================================')
        this.fetch();
    },

    addCart() {
        var info = app.getInfo();
        console.log(JSON.stringify(info));

        if (this.isCartEmpty) {
            //this.cartText = 'Cart + 1';
            //this.cartStyle = 'add-cart-text';
            this.isCartEmpty = false;
        }

        storage.set({
            key: 'storage_key',
            value: 'storage value',
            success: function() {
                console.log('call storage.set success.');
                prompt.showToast({
                    message: 'call storage.set success.',
                    duration: 2000,
                });
            },
            fail: function(data, code) {
                console.log('call storage.set fail, code: ' + code + ', data: ' + data);
            },
        });

        storage.get({
            key: 'storage_key',
            success: function(data) {
                console.log('call storage.get success: ' + data);
            },
            fail: function(data, code) {
                console.log('call storage.get fail, code: ' + code + ', data: ' + data);
            },
            complete: function() {
                console.log('call complete');
            },
        });
    },

    getFocus() {
        if (this.isCartEmpty) {
            this.cartStyle = 'cart-text-focus';
        }
        console.info('==================================================')
        console.info(app);
        //console.info(cc.version)
        console.info(ACTION_SYNC)
        console.info(this.$app.$def.globalData.appVersion)
        console.info('==================================================')
        this.fetch();
    },

    lostFocus() {
        if (this.isCartEmpty) {
            this.cartStyle = 'cart-text';
        }
    },
}